package logic

import (
	"context"
	"gorm.io/gorm"
	"product-management/model/mysql"
	"product-management/productrpc/internal/svc"
	"product-management/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodUpdatesLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodUpdatesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodUpdatesLogic {
	return &GoodUpdatesLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodUpdatesLogic) GoodUpdates(in *productrpc.GoodUpdatesRequest) (*productrpc.GoodUpdatesResponse, error) {
	// todo: add your logic here and delete this line
	var good mysql.Good
	mysql.DB.Debug().Where("user_id=?", in.UserId).Find(&good)

	data := mysql.Good{
		Model: gorm.Model{
			ID: good.ID,
		},
		UserId:  in.UserId,
		Title:   in.Title,
		Details: in.Details,
		Img:     in.Img,
	}

	mysql.DB.Updates(&data)
	return &productrpc.GoodUpdatesResponse{
		GoodId: int64(data.ID),
	}, nil
}
